<template>
  <el-form :model="detailForm" ref="detailForm" :inline="false" :rules="rules"
           :label-position="labelPosition" :class="isEdit?'':'formItem'">

    <!-- 出险相关信息 -->
    <el-card class="box-card" shadow="never">

      <div slot="header" class="header-card">
        <span class="header-card-title">出险相关信息</span>
      </div>

      <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item label="是否个人垫付:" prop="advancePayment">
            <el-select v-model="detailForm.advancePayment" placeholder="选择是否个人垫付" style="width: 100%;"
                       @change="handleChange"
                       v-if="isEdit">
              <el-option label="是" value="1"></el-option>
              <el-option label="否" value="2"></el-option>
            </el-select>
            <span v-else>{{
                detailForm.advancePayment === "1" ? '是' : detailForm.advancePayment === "2" ? '否' : ''
              }}</span>
          </el-form-item>
        </el-col>

        <el-col :span="8" v-if="detailForm.advancePayment == 1">
          <el-form-item label="个人垫付费用:" prop="advancePaymentCost">
            <el-input v-model="detailForm.advancePaymentCost" v-if="isEdit"
                      @change="changeAmount('advancePaymentCost')"  v-thousand
                      placeholder="请输入个人垫付费用"></el-input>
            <span v-else>{{ format_thousand(detailForm.advancePaymentCost) }}</span>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="备注:" prop="outDangerRemark">
            <el-input v-model="detailForm.outDangerRemark" v-if="isEdit" placeholder="请输入备注"></el-input>
            <span v-else>{{ detailForm.outDangerRemark }}</span>
          </el-form-item>

        </el-col>

      </el-row>
    </el-card>

  </el-form>
</template>

<script>
export default {
  name: 'beDangerMessage',//出险相关信息
  props: {
    value: {
      type: Object,
      default: {},
    },
    state: {
      type: String,
      default: "",
    },
  },

  computed: {
    detailForm: {
      get() {
        let that = this;
        const proxy = new Proxy(that.value, {
          get(target, key) {
            return Reflect.get(target, key);
          },
          set(target, key, value) {
            that.$emit("input", {...target, [key]: value});
            return true;
          },
        });
        return proxy;
      },
    },
    labelPosition() {
      if (this.state === 'write') {
        return 'top'
      } else {
        return 'right'
      }
    },
    isEdit() {
      let bool;
      switch (this.state) {
        case "write":
          bool = true;
          break;
        case "disabled":
          bool = false;
          break;
        case "readonly":
          bool = false;
          break;
      }
      return bool
    },
  },
  data() {
    return {
      rules: {
        advancePayment: [{required: this.state === 'write', message: '请选择是否个人垫付', trigger: 'change'}],
        dangerMaintenanceShop: [{required: this.state === 'write', message: '请输入输入维修厂', trigger: 'blur'}],
        advancePaymentCost: [{required: this.state === 'write', message: '请输入个人垫付费用', trigger: 'blur'}],
      }
    };
  },
  created() {
  },
  mounted() {
  },
  methods: {
    handleChange(val) {
      this.$set(this.value, 'advancePayment', val)
      if (val == 2) {
        this.value.advancePaymentCost = this.detailForm.advancePaymentCost = ''
      }
    },

    changeAmount(str) {
      let msg = /^(?:[1-9]\d*|0)(?:\.\d+)?$/;
      if (!msg.test(this.detailForm[str])) {
        this.detailForm[str] = "";
      } else {
        this.detailForm[str] = this.detailForm[str]
            .toString()
            .match(/^\d+(?:\.\d{0,2})?/)[0];
      }
    },
    handleEffect() {
      return new Promise((resolve, reject) => {
        this.$refs.detailForm.validate((valid) => {
          if (valid) {
            resolve();
          } else {
            this.locateToErr()
            reject();
          }
        });
      })
    },

    locateToErr() {

      this.$nextTick(() => {
        let isError = document.getElementsByClassName('is-error')
        isError[0].scrollIntoView({
          // 滚动到指定节点
          // 值有start,center,end，nearest，当前显示在视图区域中间
          block: 'center',
          // 值有auto、instant,smooth，缓动动画（当前是慢速的）
          behavior: 'smooth',
        })
      })
    },

    valid() {
      return new Promise((resolve) => {
        this.$refs.detailForm.validate((valid) => {
          resolve(valid);
        });
      });
    },
  },
};
</script>

<style lang="scss" scoped>
@import "@/styles/cwgd/formScss.scss";
</style>
